/**
 * Copyright (c) 2000-2009, Jasig, Inc.
 * See license distributed with this file and available online at
 * https://www.ja-sig.org/svn/jasig-parent/tags/rel-10/license-header.txt
 */
package e2q.portal.web.userInfo.fileImport.dataHandler;

import java.text.MessageFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import e2q.portal.web.userInfo.fileImport.bean.UpPersonDirBean;
import e2q.portal.web.userInfo.fileImport.form.UserDataImportInfoForm;

/**
 * @author smb1@cornell.edu
 * @version $Revision: 45149 $ $Date: 2009-02-25 00:46:57 -0600 (Wed, 25 Feb
 *          2009) $
 */
@Component
public class DefaultDataHandler {
	private static final Log LOG = LogFactory.getLog(DefaultDataHandler.class);

	/** Value = "user_name" */
	public static final String UNFIELD = "user_name";

	/** Value = "pswd" */
	public static final String PWDFIELD = "pswd";

	/** Value = "*LCK*" */
	public static final String ACCOUNTLOCK = "*LCK*";

	/** Value = "NULLIFY.THIS.USER" */
	public static final String NULLIFYUSER = "NULLIFY.THIS.USER";

	/** Value = "encrptd_pswd" */
	public static final String ENCRYPTPWDFIELD = "encrptd_pswd";

	/** Value = "Acc.Is.Locked" */
	public static final String ACCOUNTLOCKACKNOWLEDGE = "Acc.Is.Locked";

	/** Value = "Username \"{0}\" already exists!" */
	public static final String USER_EXISTS = "Username \"{0}\" already exists!";

	/**
	 * Value = "Password NOT changed!\r\nCurrent Password does not match stored
	 * password."
	 */
	public static final String ERRMSG_PWDNOTMATACHED = "Password NOT changed!\r\nCurrent Password does "
			+ " not match stored password.";

	protected static final String SINGLEQUOTE = "'";
	protected static final String WILDCARD = "%";

	protected static final String UPDMASK = "{0}={1}, ";

	protected static final String UPDCONDMASK = " where USER_NAME={0}";

	protected static final String COUNTUSERS = "select count( USER_NAME ) as cnt"
			+ " from UP_PERSON_DIR" + UPDCONDMASK;

	protected static final String ADDUSER = "insert into UP_PERSON_DIR ({0}) "
			+ "values ({1})";

	protected static final String UPDPWD = ("update UP_PERSON_DIR set "
			+ "ENCRPTD_PSWD={0}, LST_PSWD_CGH_DT={1} where USER_NAME={2} ")
			.toUpperCase();

	protected static final String USERSELECT = "select * from UP_PERSON_DIR {0} "
			+ "order by USER_NAME, FIRST_NAME, LAST_NAME";

	protected static final String ALLUSERS = MessageFormat.format(USERSELECT,
			new Object[] { "" });

	protected static final String GETTHISUSER = MessageFormat.format(
			USERSELECT, new Object[] { "where USER_NAME = {0} " });

	protected static final String SEARCHUSERS = MessageFormat.format(
			USERSELECT, new Object[] { "where USER_NAME like {0} "
					+ "or LAST_NAME like {0} or FIRST_NAME like {0} " });

	private static final String guest_user_name = "guest";

	@Autowired
	private RDBMUserIdentityStore rdbmuser;

	@Autowired
	@Qualifier("sessionFactory")
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	static {
		LOG.debug("USERSELECT: " + USERSELECT);
		LOG.debug("ALLUSERS: " + ALLUSERS);
		LOG.debug("GETTHISUSER: " + GETTHISUSER);
		LOG.debug("SEARCHUSERS: " + SEARCHUSERS);
		LOG.debug("ADDUSER: " + ADDUSER);
		LOG.debug("UPDMASK: " + UPDMASK);
		LOG.debug("UPDCONDMASK: " + UPDCONDMASK);
		LOG.debug("UPDPWD: " + UPDPWD);
	}// static

	public void addUser(UserDataImportInfoForm commandObject) throws Exception {

		commandObject.setErrorMsg("");
		for (UpPersonDirBean t : commandObject.getUpPersonDirBeanList()) {
			try {
				// only synchronize a non-guest request.
				if (!guest_user_name.equals(t.getUpPersonDir().getId())) {

					rdbmuser.saveUpPersonDir(t.getUpPersonDir());
					
					rdbmuser.mkIPeople(t);
				}
			} catch (RuntimeException ae) {
				// is considered to be locked, this is technically inaccurate
				commandObject.setErrorMsg(ENCRYPTPWDFIELD
						+ ACCOUNTLOCKACKNOWLEDGE);
			} catch (Exception e) {

				commandObject.setErrorMsg(MessageFormat.format(USER_EXISTS,
						new Object[] { t.getUpPersonDir().getId() }));
			}// catch

		}// while

	}// addUser

}// eoc
